﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Rosieks.Web.Flow.Metadata;

namespace Rosieks.Web.Flow
{
    public class WebflowContextProviderDictionary : Dictionary<string, IWebflowContextProvider>
    {
        static WebflowContextProviderDictionary()
        {
            WebflowContextProviderDictionary.DefaultStoreLocation = new StaticStoreLocationPolicy("TempData");
        }

        public static IStoreLocationPolicy DefaultStoreLocation { get; set; }

        public WebflowContext GetContext(WebflowModel webflowModel, ControllerContext context, string webflowName)
        {
            IStoreLocationPolicy storeLocationPolicy = webflowModel.StoreLocationPolicy ?? WebflowContextProviderDictionary.DefaultStoreLocation;

            string storeLocation = storeLocationPolicy.GetStoreLocation(null, context, StoreLocationOperation.Load);

            return this[storeLocation].GetContext(context, webflowName);
        }

        public void SetContext(WebflowModel webflowModel, ControllerContext context, WebflowContext webflowContext)
        {
            IStoreLocationPolicy storeLocationPolicy = webflowModel.StoreLocationPolicy ?? WebflowContextProviderDictionary.DefaultStoreLocation;

            string storeLocation = storeLocationPolicy.GetStoreLocation(webflowContext, context, StoreLocationOperation.Store);

            this[storeLocation].SetContext(context, webflowContext);
        }

        public void DeleteContext(WebflowModel webflowModel, ControllerContext context, WebflowContext webflowContext)
        {
            IStoreLocationPolicy storeLocationPolicy = webflowModel.StoreLocationPolicy ?? WebflowContextProviderDictionary.DefaultStoreLocation;

            string storeLocation = storeLocationPolicy.GetStoreLocation(null, context, StoreLocationOperation.Delete);

            this[storeLocation].DeleteContext(context, webflowContext);
        }
    }
}
